package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.coupon.parameter.old.BuyerInfo;
import com.ymatou.iapi.coupon.parameter.old.OrderInfo;
import com.ymatou.iapi.coupon.parameter.old.SellerOrderInfo;
import com.ymatou.iapi.coupon.parameter.old.ShoppingInfo;
import com.ymatou.iapi.couponmanage.old.parameter.CouponValue;
import com.ymatou.iapi.couponmanage.old.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.old.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.parameter.GetSellerReceiveBatchesBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.parameter.SellerReceiveBatchInfo;
import com.ymatou.iapi.couponmanage.service.GetSellerReceiveBatchesCall;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymatou.promotion.api.parameter.ChangeSellerCouponBean;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.CouponValueInfoBean;
import com.ymatou.promotion.api.parameter.CreateSellerCouponBean;
import com.ymatou.promotion.api.parameter.InvalidCouponBean;
import com.ymatou.promotion.api.parameter.ReceiveCouponInfoBean;
import com.ymatou.promotion.api.parameter.UserCouponListBean;
import com.ymatou.promotion.api.parameter.ValidateCouponInfoBean;
import com.ymatou.promotion.api.service.ChangeSellerCouponCall;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymatou.promotion.api.service.InvalidCouponCall;
import com.ymatou.promotion.api.service.ReceiveCouponInfoCall;
import com.ymatou.promotion.api.service.UserCouponListCall;
import com.ymatou.promotion.api.service.ValidateCouponInfoCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
//import com.ymttest.database.sqlwapper.YmtCouponSellerPrefixWapper;
//import com.ymttest.database.sqlwapper.YmtCouponWapper;

public class CouponCallServiceV2 {

	/**
	 * 设置默认创建优惠券的信息
	 * 
	 * @return
	 */
	public CouponBatchBean setDefaultCouponBatchBean() {
		Logger.comment("设置创建商家全场优惠券的默认CreateCouponBean");
		CouponBatchBean couponBatchBean = new CouponBatchBean();

		couponBatchBean.setSellerId(Integer.valueOf(EnvSetup
				.getData("jCouponSellerId")));
		couponBatchBean.setCouponName("自动化创建商家优惠券名称"
				+ TestDataManager.getRandomNum(5));
		couponBatchBean.setCouponDesc("自动化创建商家优惠券描述"
				+ TestDataManager.getRandomNum(10));

		List<CouponValueInfoBean> couponValues = new ArrayList<CouponValueInfoBean>();

		CouponValueInfoBean couponValue = new CouponValueInfoBean();

		couponValue.setOrderValue(new BigDecimal(2));
		couponValue.setCouponValue(new BigDecimal(1));
		couponValue.setCouponUseType(1);

		couponValues.add(couponValue);

		couponBatchBean.setCouponValues(couponValues);
		couponBatchBean.setMaxSendNum(1);
		couponBatchBean.setUserMaxReceiveNum(1);

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		couponBatchBean.setValidStart(dBefore);
		couponBatchBean.setValidEnd(dAfter);

		couponBatchBean.setScenarioType(1);

		return couponBatchBean;
	}

	/**
	 * 设置默认创建优惠券的信息
	 * 
	 * @return
	 */
	public CouponBatchBean setCouponBatchBean(int scenarioType, int maxSendNum,
			int userMaxReceiveNum) {
		CouponBatchBean couponBatchBean = new CouponBatchBean();
		couponBatchBean.setSellerId(Integer.valueOf(EnvSetup
				.getData("jCouponSellerId")));
		couponBatchBean.setCouponName("自动化创建优惠券名称"
				+ TestDataManager.getRandomNum(5));
		couponBatchBean.setCouponDesc("自动化创建优惠券描述"
				+ TestDataManager.getRandomNum(10));

		List<CouponValueInfoBean> couponValues = new ArrayList<CouponValueInfoBean>();

		CouponValueInfoBean couponValue = new CouponValueInfoBean();

		couponValue.setOrderValue(new BigDecimal(2));
		couponValue.setCouponValue(new BigDecimal(1));
		couponValue.setCouponUseType(1);

		couponValues.add(couponValue);

		couponBatchBean.setCouponValues(couponValues);
		couponBatchBean.setMaxSendNum(maxSendNum);
		couponBatchBean.setUserMaxReceiveNum(userMaxReceiveNum);

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		couponBatchBean.setValidStart(dBefore);
		couponBatchBean.setValidEnd(dAfter);

		couponBatchBean.setScenarioType(scenarioType);

		return couponBatchBean;
	}

	/**
	 * 查询商家优惠券批次
	 * 
	 * @param sellerId
	 * @return	最新的一个批次号
	 */
	public static String callGetSellerReceiveBatchesCall(int sellerId) {

		String batchCode = "";

		GetSellerReceiveBatchesBean getSellerReceiveBatchesBean = new GetSellerReceiveBatchesBean();
		GetSellerReceiveBatchesCall getSellerReceiveBatchesCall = new GetSellerReceiveBatchesCall();

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		getSellerReceiveBatchesBean.setSellerId(sellerId);
		getSellerReceiveBatchesBean.setEffectiveStartTime(dBefore);
		getSellerReceiveBatchesBean.setEffectiveEndTime(dAfter);

		getSellerReceiveBatchesCall.setData(false, getSellerReceiveBatchesBean);
		try {

			getSellerReceiveBatchesCall.callService();

			List<SellerReceiveBatchInfo> sellerReceiveBatchInfoList = getSellerReceiveBatchesCall
					.getBatchInfos().getBatchInfos();

			if (!CollectionUtils.isEmpty(sellerReceiveBatchInfoList)) {

				batchCode = sellerReceiveBatchInfoList.get(0).getBatchCode();

			}

		} catch (Exception e) {

			Logger.info(String.format(
					"###############查询商家 sellerId %s，优惠券批次失败", sellerId));
		}

		return batchCode;
	}

	/**
	 * 设置默认创建优惠券的信息
	 * 
	 * @return
	 */
	public CouponBatchBean setCouponBatchBean(int scenarioType,
			String usePlatforms, int maxSendNum, int userMaxReceiveNum) {
		CouponBatchBean couponBatchBean = setCouponBatchBean(scenarioType,
				maxSendNum, userMaxReceiveNum);

		if (usePlatforms != null) {
			couponBatchBean.setUsePlatforms(usePlatforms);
		}

		return couponBatchBean;
	}

	/**
	 * 创建满减_全场优惠券
	 * 
	 * @return BatchCode
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public String createSellerDefaultCoupon() throws ClientProtocolException,
			IOException {
		Logger.comment("创建满减_全场优惠券");
		CreateSellerCouponBean createsellercouponBean = new CreateSellerCouponBean();
		CreateSellerCouponCall createsellercouponCall = new CreateSellerCouponCall();

		CouponBatchBean couponBatchBean = setDefaultCouponBatchBean();

		createsellercouponBean.setCouponBatchBean(couponBatchBean);
		createsellercouponCall.setData(false, createsellercouponBean);
		createsellercouponCall.callService();

		return createsellercouponCall.getBatchCode();
	}

	/**
	 * 根据传入bean创建优惠券
	 * 
	 * @param couponBatchBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public CreateSellerCouponCall createSellerCoupon(
			CouponBatchBean couponBatchBean) throws ClientProtocolException,
			IOException {
		Logger.comment("根据传入bean创建优惠券");
		CreateSellerCouponBean createsellercouponBean = new CreateSellerCouponBean();
		CreateSellerCouponCall createsellercouponCall = new CreateSellerCouponCall();

		createsellercouponBean.setCouponBatchBean(couponBatchBean);
		createsellercouponCall.setData(false, createsellercouponBean);
		createsellercouponCall.callService();

		return createsellercouponCall;
	}

	/**
	 * 创建满减_商品优惠券
	 * 
	 * @return BatchCode
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public String createSellerProductCoupon() throws ClientProtocolException,
			IOException {
		Logger.comment("创建满减_商品优惠券");
		CreateSellerCouponBean createsellercouponBean = new CreateSellerCouponBean();
		CreateSellerCouponCall createsellercouponCall = new CreateSellerCouponCall();

		CouponBatchBean couponBatchBean = setDefaultCouponBatchBean();
		couponBatchBean.setScenarioType(2);

		createsellercouponBean.setCouponBatchBean(couponBatchBean);
		createsellercouponCall.setData(false, createsellercouponBean);
		createsellercouponCall.callService();

		return createsellercouponCall.getBatchCode();
	}

	/**
	 * 根据传入平台类型创建平台_满减_全场优惠券
	 * 
	 * @param usePlatforms
	 * 
	 *        1：洋码头PC端 2：洋码头WAP端 3：洋码头APP 4：神器APP 5：神器WAP
	 * 
	 * @return BatchCode
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public String createSellerCoupon(String usePlatforms)
			throws ClientProtocolException, IOException {
		Logger.comment("根据传入平台类型创建平台_满减_全场优惠券");
		CreateSellerCouponBean createsellercouponBean = new CreateSellerCouponBean();
		CreateSellerCouponCall createsellercouponCall = new CreateSellerCouponCall();

		CouponBatchBean couponBatchBean = setDefaultCouponBatchBean();
		couponBatchBean.setScenarioType(1);

		createsellercouponBean.setCouponBatchBean(couponBatchBean);
		createsellercouponCall.setData(false, createsellercouponBean);
		createsellercouponCall.callService();

		return createsellercouponCall.getBatchCode();
	}

	/**
	 * 根据传入平台类型创建平台_满减_商品优惠券
	 * 
	 * @param usePlatforms
	 * 
	 *        1：洋码头PC端 2：洋码头WAP端 3：洋码头APP 4：神器APP 5：神器WAP
	 * 
	 * @return BatchCode
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public String createSellerProductCoupon(String usePlatforms)
			throws ClientProtocolException, IOException {
		Logger.comment("根据传入平台类型创建平台_满减_商品优惠券");
		CreateSellerCouponBean createsellercouponBean = new CreateSellerCouponBean();
		CreateSellerCouponCall createsellercouponCall = new CreateSellerCouponCall();

		CouponBatchBean couponBatchBean = setDefaultCouponBatchBean();
		couponBatchBean.setScenarioType(2);
		couponBatchBean.setUsePlatforms(usePlatforms);

		createsellercouponBean.setCouponBatchBean(couponBatchBean);
		createsellercouponCall.setData(false, createsellercouponBean);
		createsellercouponCall.callService();

		return createsellercouponCall.getBatchCode();
	}

	private CreateCouponBean setDefaultPlatformCreateCouponBean()
			throws ClientProtocolException, IOException {
		CreateCouponBean createcouponBean = new CreateCouponBean();

		createcouponBean.setCouponName("自动化创建平台优惠券_"
				+ TestDataManager.getRandomNum(5));
		createcouponBean.setApplyMemo("自动化测试创建平台优惠卷");
		createcouponBean.setSendType(1);
		createcouponBean.setCouponDes("优惠券描述-自动化");

		createcouponBean.setEffectiveType(0);
		createcouponBean.setEffectiveValidType(0);
		createcouponBean.setEffectiveDays(0);

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		createcouponBean.setValidStart(dBefore);
		createcouponBean.setValidEnd(dAfter);

		createcouponBean.setCouponUseType(1);

		CouponValue couponValue = new CouponValue();
		couponValue.setOrderMinAmount(new BigDecimal(200));
		couponValue.setDeductionMaxAmount(new BigDecimal(10));

		List<CouponValue> couponValues = new ArrayList<CouponValue>();
		couponValues.add(couponValue);

		createcouponBean.setCouponValues(couponValues);
		createcouponBean.setCouponNum(2);
		createcouponBean.setPerCouponMaxUseCount(2);
		createcouponBean.setPerUserReceiveMaxCount(2);

		createcouponBean.setIsCustomCouponCode(false);
		createcouponBean.setCustomCouponCode("");

		createcouponBean.setUserType(0);

		List<Integer> usePlatforms = new ArrayList<Integer>();
		/*
		 * usePlatforms.add(1); usePlatforms.add(2); usePlatforms.add(3);
		 */

		createcouponBean.setUsePlatforms(usePlatforms);

		List<Integer> productIds = new ArrayList<Integer>();
		createcouponBean.setProductIds(productIds);

		List<Integer> sellerIds = new ArrayList<Integer>();
		createcouponBean.setProductIds(sellerIds);

		List<Integer> activityIds = new ArrayList<Integer>();
		createcouponBean.setProductIds(activityIds);

		List<Integer> brands = new ArrayList<Integer>();
		createcouponBean.setProductIds(brands);

		List<Integer> categories = new ArrayList<Integer>();
		createcouponBean.setProductIds(categories);

		createcouponBean.setApplyUser("自动化user111");
		createcouponBean.setDepartment(1);
		createcouponBean.setImpresetInvoiceId("111111");
		createcouponBean.setOperatorId(222222);

		return createcouponBean;
	}

	/**
	 * 创建平台短码优惠券默认bean
	 * 
	 * @return CreateCouponBean
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public CreateCouponBean setDefaultPlatformShortCodeCreateCouponBean()
			throws ClientProtocolException, IOException {
		Logger.comment("设置创建平台短码优惠券的默认CreateCouponBean");
		return setDefaultPlatformCreateCouponBean();
	}

	/**
	 * 创建平台链接优惠券默认bean
	 * 
	 * @return CreateCouponBean
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public CreateCouponBean setDefaultPlatformLinkCreateCouponBean()
			throws ClientProtocolException, IOException {
		Logger.comment("设置创建平台链接优惠券的默认CreateCouponBean");
		CreateCouponBean createcouponBean = setDefaultPlatformCreateCouponBean();

		createcouponBean.setPerCouponMaxUseCount(1);
		createcouponBean.setSendType(2);

		return createcouponBean;
	}

	/**
	 * 根据传入bean创建平台优惠券
	 * 
	 * @param couponBatchBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public CreateCouponCall createPlatformCoupon(
			CreateCouponBean createcouponBean) throws ClientProtocolException,
			IOException {
		Logger.comment("根据传入createcouponBean创建优惠券");

		CreateCouponCall createcouponCall = new CreateCouponCall();

		createcouponCall.setData(false, createcouponBean);
		createcouponCall.callService();

		return createcouponCall;
	}

	/**
	 * 用户领取优惠券
	 * 
	 * 需要提前创建优惠券
	 * 
	 * @param batchCode
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String receiveCouponInfo(String batchCode, int userId)
			throws ClientProtocolException, IOException {
		Logger.comment("用户" + userId + "领取优惠券，批次为" + batchCode);
		//new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode);

		ReceiveCouponInfoBean receivecouponinfoBean = new ReceiveCouponInfoBean();
		ReceiveCouponInfoCall receivecouponinfoCall = new ReceiveCouponInfoCall();

		receivecouponinfoBean.setBuyerUserId(userId);
		receivecouponinfoBean.setBatchCode(batchCode);

		receivecouponinfoCall.setData(false, receivecouponinfoBean);
		receivecouponinfoCall.callService();

		return receivecouponinfoCall.getCouponCode();
	}

	//	/**
	//	 * 用户领取默认优惠券
	//	 * 
	//	 * @param batchCode
	//	 * @param userId
	//	 * @return
	//	 * @throws ClientProtocolException
	//	 * @throws IOException
	//	 */
	//	public String receiveCouponInfo(int userId) throws ClientProtocolException,
	//			IOException {
	//		Logger.comment("用户" + userId + "领取默认优惠券");
	//		ReceiveCouponInfoBean receivecouponinfoBean = new ReceiveCouponInfoBean();
	//		ReceiveCouponInfoCall receivecouponinfoCall = new ReceiveCouponInfoCall();
	//
	//		String batchCode = createSellerDefaultCoupon();
	//		receivecouponinfoBean.setBuyerUserId(userId);
	//		receivecouponinfoBean.setBatchCode(batchCode);
	//
	//		new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode);
	//
	//		receivecouponinfoCall.setData(false, receivecouponinfoBean);
	//		receivecouponinfoCall.callService();
	//
	//		return receivecouponinfoCall.getCouponCode();
	//	}

	/**
	 * 领用优惠券<br>
	 * 创建总数为一张的 每人只能使用一次的 优惠券
	 * 
	 * @author chenjiazhu
	 * 
	 * @param sellerId 商家Id
	 * @param buyerId 买家Id
	 * @param price 优惠券金额
	 * @return BatchCode 优惠券号
	 * @throws IOException
	 * @throws ClientProtocolException
	 * 
	 * 如果获取3383的可用批次不支持单用户多次领取则，改数据库
	 * 
	 * 	select * from ymt_couponbatch where sbatchcode='1708166887'
		select * from ymt_couponsetting where iCouponSettingId='1481580'
	 * 
	 * 1. 修改总领取张数跟每个用户领取张数
	 * update Ymt_CouponSetting set iMaxUseTimePerUser=2000 where iCouponSettingId=1472087
	 * update Ymt_CouponSetting set iMaxUseTime=20000000 where iCouponSettingId=1472087
	 * 
	 * 2. 删除幂等表
	 *   delete from [YmtRelease].[dbo].[Ymt_CouponSendIdempotent]
	 */
	public String receiveCouponInfo(int sellerId, int buyerId, BigDecimal price)
			throws ClientProtocolException, IOException {
		Logger.comment("用户" + buyerId + "领取商家" + sellerId + "优惠券，券面值" + price);

		GetSellerReceiveBatchesBean getSellerReceiveBatchesBean = new GetSellerReceiveBatchesBean();
		GetSellerReceiveBatchesCall getSellerReceiveBatchesCall = new GetSellerReceiveBatchesCall();

		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Date dAfter = new Date();

		Calendar calendar1 = Calendar.getInstance(); // 得到日历
		calendar1.setTime(dNow);// 把当前时间赋给日历
		calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar1.getTime(); // 得到前一天的时间

		Calendar calendar2 = Calendar.getInstance(); // 得到日历
		calendar2.setTime(dNow);// 把当前时间赋给日历
		calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
		dAfter = calendar2.getTime(); // 得到前一天的时间

		getSellerReceiveBatchesBean.setSellerId(sellerId);
		getSellerReceiveBatchesBean.setEffectiveStartTime(dBefore);
		getSellerReceiveBatchesBean.setEffectiveEndTime(dAfter);

		getSellerReceiveBatchesCall.setData(false, getSellerReceiveBatchesBean);
		getSellerReceiveBatchesCall.callService();

		String batchCode = "";
		if (getSellerReceiveBatchesCall.getBatchInfos().getBatchInfos().size() == 0) {

			// 创建优惠券
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();

			SellerCreateBatchBean sellerCreateBatchBean = CouponServiceMCallServiceV2
					.setDefaultSellerCreateBatchBean();

			sellerCreateBatchBean.setCouponTotalCount(30000);
			sellerCreateBatchBean.setOrderMinAmount(new BigDecimal(100));
			sellerCreateBatchBean.setSellerId(sellerId);
			sellerCreateBatchBean.setCouponAmount(price);
			sellerCreateBatchBean.setReceiveNumPerUser(30000);

			SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2
					.SellerCreateBatch(sellerCreateBatchBean);
			batchCode = sellerCreateBatchCall.getBatchCode();
		} else {
			batchCode = getSellerReceiveBatchesCall.getBatchInfos()
					.getBatchInfos().get(0).getBatchCode();
		}
		return new CouponServiceMCallServiceV2()
				.receiveCouponInfo(batchCode, buyerId).getUserReceiveResult()
				.getCouponCode();
	}

	public static InvalidCouponCall inValidCoupon(String batchCode)
			throws ClientProtocolException, IOException {
		Logger.comment("作废优惠券batchCode:" + batchCode);

		InvalidCouponBean invalidcouponBean = new InvalidCouponBean();
		InvalidCouponCall invalidcouponCall = new InvalidCouponCall();

		invalidcouponBean.setBatchCode(batchCode);

		invalidcouponCall.setData(false, invalidcouponBean);
		invalidcouponCall.callService();

		return invalidcouponCall;
	}

	public static ValidateCouponInfoCall validCouponInfo(String couponCode,
			BigDecimal OrderAmount, int buyerId, int sellerId,
			String usePlatforms) throws ClientProtocolException, IOException {
		Logger.comment("验证优惠券有效性CouponCode：" + couponCode);

		ValidateCouponInfoBean validatecouponinfoBean = new ValidateCouponInfoBean();
		ValidateCouponInfoCall validatecouponinfoCall = new ValidateCouponInfoCall();

		// setBean
		validatecouponinfoBean.setCouponCode(couponCode);
		validatecouponinfoBean.setOrderAmount(OrderAmount);
		validatecouponinfoBean.setBuyerId(buyerId);
		validatecouponinfoBean.setSellerId(sellerId);

		if (usePlatforms != null)
			validatecouponinfoBean.setPlatformType(Integer
					.parseInt(usePlatforms));
		else {
			validatecouponinfoBean.setPlatformType(1);
		}

		validatecouponinfoBean.setBuyerOrderNum(0);
		validatecouponinfoBean.setUserLevel(0);

		List<com.ymatou.promotion.api.parameter.ProductInfo> productInfoList = new ArrayList<com.ymatou.promotion.api.parameter.ProductInfo>();
		validatecouponinfoBean.setProductList(productInfoList);

		validatecouponinfoCall.setData(false, validatecouponinfoBean);
		validatecouponinfoCall.callService();

		return validatecouponinfoCall;
	}

	/**
	 * 获取买家我的优惠券
	 * 
	 * @param buyerUserId
	 * @param couponState
	 *        优惠券状态
	 *        0：全部
	 *        1：全部
	 *        2：未生效
	 *        3：已使用
	 *        4：已过期
	 *        5：已作废
	 * @param pageSize 每页显示记录数
	 * @param curPage 当前页
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static UserCouponListCall getUserCouponList(int buyerUserId,
			int couponState, int pageSize, int curPage)
			throws ClientProtocolException, IOException {
		Logger.comment("获取买家我的优惠券");

		UserCouponListBean usercouponlistBean = new UserCouponListBean();
		UserCouponListCall usercouponlistCall = new UserCouponListCall();

		usercouponlistBean.setBuyerUserId(buyerUserId);
		usercouponlistBean.setCouponState(couponState);
		usercouponlistBean.setPageSize(pageSize);
		usercouponlistBean.setCurPage(curPage);
		usercouponlistCall.setData(false, usercouponlistBean);
		usercouponlistCall.callService();

		return usercouponlistCall;
	}

	public static ChangeSellerCouponCall ChangeSellerCoupon(String batchCode,
			String productIds) throws ClientProtocolException, IOException {
		Logger.comment("编辑商家商品优惠券对应的商品，BaychCode=" + batchCode
				+ ",ProductCodes=" + productIds);

		ChangeSellerCouponBean changesellercouponBean = new ChangeSellerCouponBean();
		ChangeSellerCouponCall changesellercouponCall = new ChangeSellerCouponCall();

		Logger.comment("创建单个商品，根据batchCode以及商品的Id设置Bean参数");

		changesellercouponBean.setBatchCode(batchCode);

		changesellercouponBean.setProductCodes(productIds);

		changesellercouponCall.setData(false, changesellercouponBean);
		changesellercouponCall.callService();

		return changesellercouponCall;
	}

	public static ShoppingInfo setDefaultShoppingInfo(int buyerId,
			String platformCouponCode, String sellerCouponCode) {
		Logger.comment("设置默认的购物车");
		ShoppingInfo shoppingInfo = new ShoppingInfo();

		BuyerInfo buyerInfo = new BuyerInfo();

		buyerInfo.setBuyerId(buyerId);
		// 默认下单数0
		buyerInfo.setBuyerOrderNum(0);

		shoppingInfo.setBuyerInfo(buyerInfo);

		SellerOrderInfo sellerOrderInfo = new SellerOrderInfo();

		// 卖家Id
		int sellerId = Integer.parseInt(EnvSetup.getData("jCouponSellerId"));
		sellerOrderInfo.setSellerId(sellerId);

		if (sellerCouponCode != null && sellerCouponCode != "") {
			sellerOrderInfo.setCouponCode(sellerCouponCode);
		}

		com.ymatou.iapi.coupon.parameter.old.OrderInfo orderInfo = new com.ymatou.iapi.coupon.parameter.old.OrderInfo();
		com.ymatou.iapi.coupon.parameter.old.ProductInfo productInfo = new com.ymatou.iapi.coupon.parameter.old.ProductInfo();

		String productCode = TestDataManager.getRandomNum(36);
		productInfo.setProductCode(productCode);
		productInfo.setActivityId(0);
		productInfo.setBrandId(2111);
		productInfo.setCategoryId(3111);
		productInfo.setProductQuantity(1);
		productInfo.setProductPrice(new BigDecimal(1000));

		List<com.ymatou.iapi.coupon.parameter.old.ProductInfo> productInfos = new ArrayList<com.ymatou.iapi.coupon.parameter.old.ProductInfo>();
		productInfos.add(productInfo);
		orderInfo.setProductInfos(productInfos);

		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		orderInfos.add(orderInfo);

		sellerOrderInfo.setOrderInfos(orderInfos);

		List<SellerOrderInfo> sellerOrderInfos = new ArrayList<SellerOrderInfo>();
		sellerOrderInfos.add(sellerOrderInfo);

		shoppingInfo.setSellerOrderInfos(sellerOrderInfos);

		if (platformCouponCode != null && platformCouponCode != "") {
			shoppingInfo.setPlatformCouponCode(platformCouponCode);
		}

		shoppingInfo.setPlatformUsage(1);

		return shoppingInfo;
	}

	//	public static int useCoupon(int userId, String couponCode) {
	//		return new YmtCouponWapper().useCoupon(userId, couponCode, null);
	//	}

	//	public static int useCouponV2(int userId, String couponCode) {
	//		return new couponWapper().useCouponV2(userId, couponCode, null);
	//	}

	public static void main(String args[]) {
		try {

			// CouponCallService couponCallService = new CouponCallService();
			// // //Logger.debug(new CouponCallService().ReceiveCouponInfo(288484,1113,new
			// BigDecimal(100)));
			// //
			// // CouponBatchBean couponBatchBean = couponCallService.setCouponBatchBean(1,"1",2,1);
			// // String batchCode =
			// couponCallService.createSellerCoupon(couponBatchBean).getBatchCode();
			//
			// // couponCallService.ReceiveCouponInfo("1604156903", 1113);
			//
			// // couponCallService.ReceiveCouponInfo(1113);
			//
			// CreateCouponBean createCouponBean =
			// couponCallService.setDefaultCreatePlatformCouponBean();
			// CreateCouponCall createCouponCall =
			// couponCallService.createPlatformCoupon(createCouponBean);
			//
			int userId = Integer.valueOf(EnvSetup.getData("jCouponSellerId"));
			// couponCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);

			// Logger.debug(batchCode);

			// int sellerId = 288484;
			// int userId = 1113;
			// BigDecimal couponValue = new BigDecimal(10);
			// BigDecimal orderValue = new BigDecimal(100);
			// // String couponCode = new CouponCallService().receiveCouponInfo(sellerId, userId,
			// // couponValue);
			//
			// new CouponCallService().validCouponInfo("164617611720238699XCOdT3iO", orderValue,
			// userId, sellerId, null);
			//int userId = 20323545;
			// UserCouponListCall userCouponListCall = new
			// CouponCallService().getUserCouponList(userId, 1, 100, 1);
			//
			// UserCouponListData result = userCouponListCall.getUserCouponListData();
			//
			// List<MyCouponInfoBean> couponList = result.getUserCouponList();
			//
			// for (int i = 0; i < couponList.size(); i++) {
			// MyCouponInfoBean MyCouponInfoBean = couponList.get(0);
			// System.out.println(MyCouponInfoBean.getCouponCode());
			// }
			/**创建商家券
			            CouponCallService couponCallService = new CouponCallService();

			            CouponBatchBean bean = couponCallService.setDefaultCouponBatchBean();
			            // bean.getCouponValues().get(0).setOrderValue(orderValue);
			            //bean.setSellerId(20227136);
			            bean.setSellerId(512005);
			           bean.setMaxSendNum(999999999);
			            bean.setMaxSendNum(999999999);
			            bean.setUserMaxReceiveNum(999999999);
			            bean.setCouponName("StressTest_20160808_09");
			             bean.getCouponValues().get(0).setOrderValue(new BigDecimal(100));
			             bean.getCouponValues().get(0).setCouponValue(new BigDecimal(10));
			             
			            CreateSellerCouponCall createSellerCouponCall = couponCallService.createSellerCoupon(bean);

			//            String couponCode = couponCallService.receiveCouponInfo(createSellerCouponCall.getBatchCode(), userId);
			            System.out.println(createSellerCouponCall.getBatchCode());
			 
			            couponCallService.receiveCouponInfo(createSellerCouponCall.getBatchCode(), 1223344);
			            
			            */
			/**
			 * 创建平台券*/

			//            CouponCallService couponCallService = new CouponCallService();
			//            CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformLinkCreateCouponBean() ;   
			//            createCouponBean.setCouponNum(999999999);
			//            createCouponBean.setPerCouponMaxUseCount(1);
			//            createCouponBean.setPerUserReceiveMaxCount(999999999);
			//            
			//            Date dNow = new Date(); // 当前时间
			//            Date dBefore = new Date();
			//            Date dAfter = new Date();
			//
			//            Calendar calendar1 = Calendar.getInstance(); // 得到日历
			//            calendar1.setTime(dNow);// 把当前时间赋给日历
			//            calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
			//            dBefore = calendar1.getTime(); // 得到前一天的时间
			//
			//            Calendar calendar2 = Calendar.getInstance(); // 得到日历
			//            calendar2.setTime(dNow);// 把当前时间赋给日历
			//            calendar2.add(Calendar.DAY_OF_MONTH, 720); // 设置为后十天
			//            dAfter = calendar2.getTime(); // 得到前一天的时间
			//
			//            createCouponBean.setValidStart(dBefore);
			//            createCouponBean.setValidEnd(dAfter);
			//            
			//            CouponValue couponValue = new CouponValue();
			//            couponValue.setOrderMinAmount(new BigDecimal(61));
			//            couponValue.setDeductionMaxAmount(new BigDecimal(60));
			//
			//            List<CouponValue> couponValues = new ArrayList<CouponValue>();
			//            couponValues.add(couponValue);
			//
			//            createCouponBean.setCouponValues(couponValues);
			//            
			//            CreateCouponCall createCouponCall =couponCallService.createPlatformCoupon(createCouponBean);
			//            System.out.println(createCouponCall.getBatchCode());

			// 领取
			CouponCallServiceV2 couponCallService = new CouponCallServiceV2();

			couponCallService.receiveCouponInfo(3383, 20238699, new BigDecimal(
					1));

			//            for(int i=0;i<20;i++)
			//            {
			////            	System.out.println(couponCallService.receiveCouponInfo("1609086921", 20324128));
			//            	System.out.println(couponCallService.receiveCouponInfo("1609233563", 20568095 ));
			//            }

			//            couponCallService.receiveCouponInfo(288484,20238699,new BigDecimal(1));

			//            String batchCodes[] = {"1603300335","1603300336","1603300337","1603300338","1603300339","1603300340","1604150720","1611145987","1611145990","1611145993","1611146007","1611187894","1611187896","1611187899","1611187933","1611187952","1611187953","1611187954","1611187955","1611187957"};
			//            
			//            for (String batchCode : batchCodes) {
			//            	 couponCallService.inValidCoupon(batchCode);
			//                 
			//			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
